package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.MyBaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R list(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,MyBaseContext.getId()).orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){

        Long id = MyBaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,id);
        if (null!=shoppingCart.getSetmealId()) {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else{
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(lqw);
        if (null!=shoppingCartOne) {
            shoppingCartOne.setNumber(shoppingCartOne.getNumber()+1);
            boolean update = shoppingCartService.update(shoppingCartOne, lqw);
            if (update) {
                return R.success(shoppingCartOne);
            }
            return R.error("修改失败");
        }

        shoppingCart.setUserId(id);
        shoppingCart.setCreateTime(LocalDateTime.now());
        boolean b = shoppingCartService.save(shoppingCart);
        if (b) {
            return R.success(shoppingCart);
        }
        return R.error("保存失败");
    }
    @DeleteMapping("/clean")
    public R clean(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,MyBaseContext.getId());
        boolean b = shoppingCartService.remove(lqw);
        if (b) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,MyBaseContext.getId());
        if (null!=shoppingCart.getSetmealId()) {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else{
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(lqw);
        if (null!=shoppingCartOne) {
            if (shoppingCartOne.getNumber() >1) {
                shoppingCartOne.setNumber(shoppingCartOne.getNumber()-1);
                boolean update = shoppingCartService.update(shoppingCartOne, lqw);
                if (update) {
                    return R.success(shoppingCartOne);
                }
                return R.error("删除失败");
            }
        }
        boolean b = shoppingCartService.remove(lqw);
        if (b) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }
}
